home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / hypercar / xcmd / xrulesde.sit / Xrules™ Tutorial / card_10768.txt < prev    next >
Encoding:
Text File  |  1991-06-24  |  7.3 KB  |  112 lines

  1. -- card: 10768 from stack: in
  2. -- bmap block id: 11103
  3. -- flags: 4000
  4. -- background id: 7050
  5. -- name: Daemons
  6.  
  7.  
  8. -- part 1 (field)
  9. -- low flags: 01
  10. -- high flags: 0007
  11. -- rect: left=2 top=26 right=314 bottom=511
  12. -- title width / last selected line: 0
  13. -- icon id / first selected line: 0 / 0
  14. -- text alignment: 0
  15. -- font id: 22
  16. -- text size: 10
  17. -- style flags: 0
  18. -- line height: 13
  19. -- part name: doc
  20.  
  21.  
  22. -- part contents for card part 1
  23. ----- text -----
  24. Next, we will examine daemons.  Daemons are evaluated when any fact used by them is asserted.  They provide a mechanism for asserting facts based on the assertion of other facts.  In other words, if a fact is asserted that implies the state of other facts, these other facts can be asserted via a daemon.  Daemons appear much like rules but, unlike rules, a daemon will evaluate facts as they are at the time that the daemon is examined.  The inference engine will never attempt to assert a fact while processing a daemon, as it will when processing a rule while backward chaining.  Neither will it examine rules using a fact set by a daemon the way that it will for facts set by rules when forward chaining.  Thus, daemons do not affect the search strategy of the inference engine directly, the way that rules do.  Instead, the inference engine will cease evaluating the daemon when it encounters a fact that is not asserted and will only examine other daemons when the daemon being processed asserts a fact. 
  25.  
  26. The logic for daemons can be expressed as follows:
  27.  
  28. process daemons(fact)
  29.   set pointer to top of rule base
  30.   while daemons remain
  31.     get next daemon
  32.     if daemon uses fact then
  33.       evaluate daemon
  34.       if daemon evaluates true then
  35.         if daemon asserts new fact then
  36.           assert fact
  37.           process daemons(new fact)
  38.         end if
  39.       end if
  40.     end if
  41.   end while
  42. end daemon
  43.  
  44. Note that the call to "process daemons" within the routine "process daemons" is a recursive call and that it passes in the newly asserted fact.  The system will process daemons using a fact when that fact is asserted, regardless of how the fact was asserted.  However, even when forward chaining, the system will not process rules using a fact when that fact is asserted by a daemon. 
  45.  
  46. We will now set up an example with daemons.  You may walk through this example using the shell.  Go to the "tutorial shell" card.  Click on the "load RB" button to make certain that the rule base is loaded in the "Rule Base" field.  Next if the "Forward Chain/Backward Chain" field does not show "Backward Chain", click on it to toggle it to "Backward Chain". 
  47.  
  48. Now modify the rule base as follows:
  49.  
  50. Add a query for the fact "car" as shown below.
  51.  
  52.   fact
  53.     car
  54.       "the vehicle is a car, true or false?",
  55.     passengers
  56.       "How many passengers can it carry?",
  57.     wheels
  58.       "How many wheels does it have",
  59.     "used for cargo" 
  60.       "Is it used to carry cargo?",
  61.     make
  62.       "What is the make of the vehicle?"
  63.  
  64. The query for fact "car" will wrap in the "Rule Base" field on the "tutorial shell" card.  You should let it wrap.  DO NOT place a carriage return inside of the quotes.
  65.  
  66. Then add the rule "truck" and daemons "cargo 1" and "cargo 2."
  67.  
  68.   rule truck
  69.   if 
  70.     fact wheels >= 4 and
  71.     fact "used for cargo" = yes
  72.   then 
  73.     put "truck" into fact model
  74.     conclude "The vehicle is a truck."
  75.  
  76.   daemon "cargo 1"
  77.   when 
  78.     not fact car
  79.   then 
  80.     put yes into fact "used for cargo"
  81.  
  82.   daemon "cargo 2"
  83.   when 
  84.     fact car
  85.   then 
  86.     put no into fact "used for cargo"
  87.  
  88. If you would like to monitor the process, click on the "trace" check box to set trace on (an X will show in the box when it is on).  To step through the process, click on the "step" check box to set step on (an X will show in the box when it is on). 
  89.  
  90. Next, click on the "(re)start" button to start the session and answer the questions or follow the directions as they appear in the "Query" field.  You should enter "model" in response to the "Enter goal..." query.  During the session you may toggle between this card and others in the stack.  The shell will retain its state when you toggle among cards; you do not have to start over each time that you return to the shell.
  91.  
  92. If you checked both trace and step, watch the "Trace" field to see the actions taken by the system.  The system will show the state of the stack in the "Stack" field and the facts and their values in the "Facts" field. 
  93.  
  94. When the system asks whether the vehicle is a car, answer "false".  The system then  asserts fact "car" and pushes fact "car" on the stack for daemon processing.  Fact "car" now appears on the stack twice; once for processing the fact and once for processing the daemons using that fact.  
  95.  
  96. The first daemon using "car" is daemon "cargo 1", which is also pushed on the stack.  Since fact "car" is false, the daemon proves true and the system asserts fact "used for cargo" as "yes".  Fact "used for cargo" is then pushed on the stack to process daemons using it.  Since there are no daemons using fact "used for cargo", it is popped from the stack.  Since daemon "cargo 1" is finished, it is also popped from the stack.
  97.  
  98. The system then resumes the search for daemons using fact "car", finds daemon 
  99. "cargo 2", pushes it on the stack, and evaluates it.  Since fact "car" is false, this daemon proves false and nothing is asserted. The daemon is then popped from the stack.  After all of the daemons are processed, fact "car" for processing daemons is popped from the stack.  Since, fact "car" has been asserted, it is also popped from the stack.
  100.  
  101. The system completes rule "Corvette" and progresses through rules "Camaro", and 
  102. "Mustang" finding none of the rules true.  When it reaches rule "truck", it will first ask about fact "wheels" by prompting "How many wheels does it have?" Answer "4".  The system will then assert fact "wheels = 4", find the clause about wheels true and examine the clause using the fact "used for cargo".  It finds that fact "used for cargo" is set to "yes" (as set by the daemon), causing that clause and the rule to prove true.  It then asserts fact "model" and concludes "The vehicle is a truck.". 
  103.  
  104. Now, retry the sequence, only this time answer "true" to the question "The vehicle is a car, true or false?"  You will see that the system again processes daemon "cargo 1" when fact car is set, only this time the daemon fails to prove true.  The system then processes daemon "cargo 2" and, since fact "car" is set to "true", the daemon proves true and fact "used for cargo" is set to "no".
  105.  
  106. The system then resumes processing rule "Corvette" and processes rules "Camaro" and "Mustang" until it finds one of them true.  If it gets to rule "truck", it finds that rule false because fact "used for cargo" was set to "no" by the daemon "cargo 2".
  107.  
  108. Daemons operate the same whether you are forward or backward chaining.  That is, they are evaluated any time that a fact used by them is asserted, regardless of how the fact was asserted or what value was assigned to it.  Note that in the above example, we use fact "wheels" to force the system to examine rule "truck" when forward chaining.  Otherwise, the system would never examine the rule since the fact "used for cargo" was established by a daemon rather than via query or another rule.
  109.  
  110. Feel free to modify the rule base on the "tutorial shell" card and experiment with daemons.  If you wish to restore the original sample rule base, simply click on the "load RB" button.
  111.  
  112.